package com.algorithm.practice.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.algorithm.practice.R;

import java.util.regex.Pattern;

public class ArrayFragment extends Fragment {

    private static final String TAG = "ArrayFragment";

    static {
        System.loadLibrary("test-array");
    }

    private static final String[] SORT_METHOD = {"冒泡","选择","插入","归并","快速","堆排序"};

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_array, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tip = view.findViewById(R.id.frag_array_tip);
        tip.setText("排序测试");
        editText = view.findViewById(R.id.layout_input_edit);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        Button button = view.findViewById(R.id.layout_input_btn);
        RadioGroup radioGroup = view.findViewById(R.id.frag_array_rg);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.frag_array_rb:
                        type = 0;
                        break;
                    case R.id.frag_array_rb2:
                        type = 1;
                        break;
                }
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = editText.getEditableText().toString().trim();
                Pattern pattern = Pattern.compile("^[0-9]+$");
                if (pattern.matcher(s).matches()) {
                    try {
                        tip.setText(sort(s, type, method));
                    } catch (Exception e) {
                        Log.e(TAG, e.getMessage());
                    }
                    editText.setText("");
                } else {
                    editText.setHint("请输入有效的数字串");
                }
            }
        });
        Spinner spinner = view.findViewById(R.id.frag_array_spinner);
        spinner.setAdapter(new ArrayAdapter<>(spinner.getContext(), R.layout.item_array_frag_spinner, SORT_METHOD));
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                method = (byte) position;
                Log.e(TAG, "methodId:" + method);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private byte type = 0;//0,up;1,down

    private byte method = 0;

    private TextView tip;

    private EditText editText;

    private native String sort(String number, byte type, byte method);
}
